package com.rcloud.server.sealtalk.util.market;

import com.rcloud.server.sealtalk.configuration.SealtalkConfig;
import com.rcloud.server.sealtalk.constant.ConstantsURL;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class AliMarkApiClient {
    @Resource
    protected SealtalkConfig sealtalkConfig;

    /**
     * 印刷文字识别–烟草零售专卖许可证识别
     */
    public String recognizeCigaretteCert(String regUrl) {
        String host = ConstantsURL.cigaretteHost;
        Map<String, String> headers = new HashMap<>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + sealtalkConfig.getAppCode());
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("IMAGE", regUrl);
        bodys.put("IMAGE_TYPE", "1");
        try {
            HttpResponse response = HttpUtils.doPost(host, "", headers, querys, bodys);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String result = EntityUtils.toString(response.getEntity());
                log.info("烟草零售专卖许可证识别成功: {}", result);
                return result;
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error("烟草零售专卖许可证识别失败: {}", e.getMessage());
            return null;
        }
    }

    /**
     * 烟草专卖零售许可证查询
     *
     * @param certNum 中国烟草专卖零售许可证编号
     */
    public String queryTobacooCert(String certNum) {
        //API产品路径
        String host = ConstantsURL.queryTobacoo;
        String method = "GET";
        //阿里云APPCODE
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + sealtalkConfig.getAppCode());
        Map<String, String> querys = new HashMap<>();
        //参数配置
        //中国烟草专卖零售许可证编号，如：370921106436
        querys.put("STRING", certNum);
        try {
            HttpResponse response = HttpUtils.doGet(host, "", headers, querys);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String result = EntityUtils.toString(response.getEntity());
                log.info("烟草专卖零售许可证查询成功: {}", result);
                return result;
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error("烟草专卖零售许可证查询失败: {}", e.getMessage());
            return null;
        }
    }

    /**
     * 食品许可证识别
     */
    public String foodLicenseIdentification(String imageUrl) {
        String host = ConstantsURL.foodLicenseIdentificationHost;
        Map<String, String> headers = new HashMap<>();
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + sealtalkConfig.getAppCode());
        Map<String, String> bodys = new HashMap<>();
        Map<String, String> querys = new HashMap<>();
        bodys.put("IMAGE", imageUrl);
        bodys.put("IMAGE_TYPE", "1");
        try {
            HttpResponse response = HttpUtils.doPost(host, "", headers, querys, bodys);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String result = EntityUtils.toString(response.getEntity());
                log.info("食品许可证识别成功: {}", result);
                return result;
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error("烟草专卖零售许可证查询失败: {}", e.getMessage());
            return null;
        }
    }

    //    public static void main(String[] args) {
//        String host = "https://cigarette.market.alicloudapi.com";
//        String path = "/ai_market/intelligent_tobacco_retail_license_identification/v1";
//        String method = "POST";
//        String appcode = "c5ffadc3c470446d9f5eaa928e851e64";
//        Map<String, String> headers = new HashMap<String, String>();
//        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
//        headers.put("Authorization", "APPCODE " + appcode);
//        //根据API的要求，定义相对应的Content-Type
//        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//        Map<String, String> querys = new HashMap<String, String>();
//        Map<String, String> bodys = new HashMap<String, String>();
//        bodys.put("IMAGE", "https://icredit-api-market.oss-cn-hangzhou.aliyuncs.com/%E8%89%BE%E7%A7%91%E7%91%9E%E7%89%B9_%E5%8D%B0%E5%88%B7%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB_%E7%83%9F%E8%8D%89%E9%9B%B6%E5%94%AE%E4%B8%93%E5%8D%96%E8%AE%B8%E5%8F%AF%E8%AF%81%E8%AF%86%E5%88%AB/%E7%83%9F%E8%8D%89%E4%B8%93%E5%8D%96%E9%9B%B6%E5%94%AE%E8%AE%B8%E5%8F%AF%E8%AF%81.jpg");
//        bodys.put("IMAGE_TYPE", "1");
//        try {
//            HttpResponse response = HttpUtils.doPost(host, path, headers, querys, bodys);
//            System.out.println(response.toString());
//            int statusCode = response.getStatusLine().getStatusCode();
//            if (statusCode == 200) {
//                String result = EntityUtils.toString(response.getEntity());
//                log.info("烟草许可证识别成功: {}", result);
//            }
//            //获取response的body
//            //System.out.println(EntityUtils.toString(response.getEntity()));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    public static void main(String[] args) {
        //API产品路径
        String host = "http://tobacoo.market.alicloudapi.com";
        String path = "/icredit_ai_market/tobacco_monopoly/v1";
        String method = "GET";
        //阿里云APPCODE
        String appcode = "c5ffadc3c470446d9f5eaa928e851e64";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        //参数配置
        //中国烟草专卖零售许可证编号，如：370921106436
        querys.put("STRING", "422801119593");
        try {
            HttpResponse response = HttpUtils.doGet(host, path, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //todo  增加一个烟草证识别的接口功能，识别之后进行存储
}
